<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_contract_statements".
 *
 * @property string $id
 * @property integer $contract_id
 * @property string $name
 * @property integer $createtime
 * @property string $pirce
 */
class BusinessContractStatements extends \yii\db\ActiveRecord
{


    const STATUS_DEFAULT = 0;
    const STATUS_AGREE = 1;
    public static $statues = [
        self::STATUS_DEFAULT => '未审核',
        self::STATUS_AGREE => '已审核',

    ];


    const NOPAY = 0;
    const HADPAY = 1;
    public static $pays = [
        self::NOPAY => '未付款',
        self::HADPAY => '已付款',
    ];

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '合同购结算单',      //用于修改了什么的显示
        'mainfield' => 'name',      //主要的显示标题的字段
        'picturefield' => ['file'],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_contract_statements';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['contract_id', 'number', 'price'], 'required'],
            [['contract_id', 'createtime','num','agreetime','inputtime','status','ispay'], 'integer'],
            [['price'], 'number'],
            [['name'], 'string', 'max' => 30],
            [['number','connect_address'], 'string', 'max' => 50],
            [['agreeuser','inputuser','connect_person','connect_tel'], 'string', 'max' => 20],
            ['remark','string','max'=>500],
            [['file'], 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']],
            [['purchases'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'contract_id' => '合同',
            'name' => '结算单名称',
            'createtime' => '创建时间',
            'price' => '金额',
            'file' => '附件',
            'purchases' => '要结算的购进单',
            'num' => '结算数量',
            'number' => '结算单号',
            'agreeuser' => '审核人',
            'agreetime' => '审核时间',
            'remark' => '备注',
            'inputuser' => '数据填写人',
            'inputtime' => '数据填写时间',
            'connect_person' => '购进联系人',
            'connect_tel' => '联系电话',
            'connect_address' => '联系地址',
            'status' => '状态',
            'ispay' => '是否付款'
        ];
    }

    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
            $this->inputuser = Yii::$app->user->identity->username;
            $this->inputtime = time();
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @return $this
     * 关联购进单
     */
    public function getPurchas()
    {
        return $this->hasMany(BusinessContractPurchase::className(),['sta_id'=>'id']);
    }


    /**
     * @return \yii\db\ActiveQuery
     * 关联的合同
     */
    public function getContract()
    {
        return $this->hasOne(BusinessContract::className(),['id'=>'contract_id']);
    }


    public function getAdjustment()
    {
        return $this->hasMany(BusinessContractAdjustment::className(),['sta_id'=>'id']);
    }
}
